﻿function dialogShow(data, title, width, height) {
    if ($("#dv_dialog").length == 0) {
        $("<div/>").attr("id", "dv_dialog").appendTo($("body"));
        $("#dv_dialog").dialog({
            autoOpen: false,
            width: 420,
            height: 200,
            modal: true,
            resizable: false,
            position: "top"
        });    
    }
    $("#dv_dialog").html(data).dialog("open");
    $("#dv_dialog").dialog("option", { title: title, width: width, height: height });
}

function dialogHide() {
    $("#dv_dialog").dialog("close");
}

function showProgress() {
    $.ajax({
        type: "get",
        url: "progress.ashx",
        data: { uploadguid: $("#Webb_Upload_GUID").val(), r: Math.random() },
        dataType: "json",
        success: function(data) {
            if (data.done == 1) {
                clearInterval(progressInterval);
                showImportScorm();
            }
            else {
                dialogShow(getHtml(data), "上传课件进度 " + data.percent + " %",400,200);
            }
        }
    });
}

function showImportScorm() {
    var data = "<div class=\"popupContent\"><div style=\"padding:10px;\">正在导入课程，请耐心等候……</div></div>";
    dialogShow(data, "导入课程章节",400,100);
}

var submited = false;
var progressInterval = null;
function openProgress() {
    if (!submited) {
        var openBar = $(":file").length > 0;

        if (openBar) {
            var progressHtml = getHtml({ progress: 0, status: '加载中...', filename: '', percent: 0, speed: '', lefttime: '' });
            dialogShow(progressHtml, "上传课件进度 0 %",400,200);
            progressInterval = setInterval("showProgress();", 1000);
            submited = true;
        }
        return true;
    }
    else {
        event.srcElement.disabled = true;
        return false;
    }
}

function getHtml(options) {
    var webData = '<div style="padding:10px 10px 0 10px;"> \
                        <strong>状态：</strong> ' + options.status + ' \
                    </div> \
                    <div style="padding:0 10px;"> \
                        <strong>文件名：</strong> ' + options.filename + ' \
                    </div> \
                    <div style="padding:3px 10px 3px 10px;"> \
                        <div class="loadbg"> \
                            <div class="load" style="width:' + options.percent + '%"></div> \
                        </div> \
                    </div> \
                    <div style="padding:0 10px;"> \
                        <strong>传输速率：</strong> ' + options.speed + ' \
                    </div> \
                    <div style="padding:0 10px;"> \
                        <strong>剩余时间：</strong> ' + options.lefttime + ' \
                    </div> \
                    <div style="text-align:right;margin:10px 0px;padding-right:5px;"> \
                        <input class="btn" disabled="disabled" type="button" value=" 确定 " /> \
                        <input class="btn" type="button" value=" 取消 " onclick="window.location.href=window.location.href;" /> \
                    </div> \
                    ';
    return webData;
}